<?php
class newsController extends PublicController
{   
    public function getListAction(){
        $ob = D('news');
        $newsType_ob = D('newsType');
        $page = isset($_GET['page'])?$_GET['page']:1;
        $page_num = 5;
        $where = '';
        $data = $ob->getPageNewsList($page,$page_num,$where=null);
        $count = $data['count'];
        $page_ob = new page($count, $page,$page_num);
        $page_str = $page_ob->getPageBlock(2);
        
        $type_arr = $newsType_ob->getAllTypeList();
        $this->assign('type_arr', $type_arr);
        $this->assign('page_str', $page_str);
        $this->assign('data', $data['value']);
        $this->display('news_list.html');
    }
    public function addAction(){
        $ob = D('news');
        $newsType_ob = D('newsType');
        $type_arr = $newsType_ob->getAllTypeList();
        $this->assign('type_arr', $type_arr);
        if(!empty($_POST)){
            $title = $_POST['title'];
            $type = $_POST['type'];
            $author = $_POST['author'];
            if(empty($title)){
                $this->success(APP.'/news/add.html', '标题不能为空');
            }
            if(empty($type)){
                $this->success(APP.'/news/add.html', '未选择类型');
            }
            if(empty($author)){
                $this->success(APP.'/news/add.html', '作者不能为空');
            }
            $arr=array(
                'title'=>$_POST['title'],
                'type'=>$_POST['type'],
                'author'=>$_POST['author'],
                'publish_time'=>time(),
                'update_time'=>time(),
                'content'=>$_POST['content'],
                'remark'=>$_POST['remark']
            );
            $re = $ob->insert($arr);
            if($re){
                $this->success(APP.'/news/getList.html', '添加文章成功');
            }else {
                $this->success(APP.'/news/add.html', '添加文章失败');
            }
        }else {
            $this->display('news_add.html');
        }
    }
    public function delAction(){
        $ob = D('news');
        $id = get('id');
        $re = $ob->where("id={$id}")->delete();
        if($re){
            $this->redirect(APP.'/news/getList.html','删除成功');
        }else{
            $this->redirect(APP.'/news/getList.html','删除失败');
        }
    }
    public function editAction(){
        $ob = D('news');
        $newsType_ob = D('newsType');
        $id = get('id');
        if(empty($_POST)){
            $data = $ob->getNewsById($id);
            if(empty($data)){
                $this->redirect(APP.'/news/getList.html','这条新闻不存在');
            }else {
                $type_arr = $newsType_ob->getAllTypeList();
                $this->assign('type_arr', $type_arr);
                $this->assign('data', $data);
                $this->display('news_edit.html');
            }
        }else {
            $title = $_POST['title'];
            $type = $_POST['type'];
            $author = $_POST['author'];
            if(empty($title)){
                $this->success(APP."/news/edit-id-$id.html", '标题不能为空');
            }
            if(empty($type)){
                $this->success(APP."/news/edit-id-$id.html", '未选择类型');
            }
            if(empty($author)){
                $this->success(APP."/news/edit-id-$id.html", '作者不能为空');
            }
           $arr=array(
                'title'=>$_POST['title'],
                'type'=>$_POST['type'],
                'author'=>$_POST['author'],
                'update_time'=>time(),
                'content'=>$_POST['content'],
                'remark'=>$_POST['remark']
            );
            $re = $ob->where("id='{$id}'")->update($arr);
            if($re){
                $this->success(APP.'/news/getList.html', '编辑文章成功');
            }else {
                $this->success(APP."/news/edit-id-$id.html", '编辑文章失败');
            }
        }
    }
}